package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.CarUsersRegWayEnum;
import cn.sykj.car.dict.CarUsersUserFalgEnum;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.entity.CarStore;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.ICarUsersService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.CarStoreVO;
import cn.sykj.car.vo.CarUsersVO;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户注册信息 前端控制器
 * </p>
 *
 * @author duanzis
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/carUsers")
@Slf4j
public class CarUsersController extends BaseController{

    @Resource
    private IUsersService iUsersService;

    @Resource
    private ICarUsersService iCarUsersService;

    /**
     * 会员信息查询
     *
     * @param carUsersVO
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "会员信息查询", notes = "会员信息查询")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carUsers:view")
    @RequestMapping(value = "/pageCarUsers", method = RequestMethod.POST)
    public ResponseBean pageCarUsers(@RequestBody CarUsersVO carUsersVO,
                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) throws Exception {


        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        IPage<CarUsersVO> page = new Page(currentPage, pageSize);
        page = iCarUsersService.pageCarUsers(page, carUsersVO);
        return new ResponseBean(StatusCode.OK, page);
    }

    /**
     * 导出会员信息
     *
     * @param carUsersVO
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "导出会员信息", notes = "导出会员信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carUsers:export")
    @RequestMapping(value = "/exportCarUsers", method = RequestMethod.POST)
    public void exportCarUsers(@RequestBody CarUsersVO carUsersVO, HttpServletResponse response
    ) throws Exception {
        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        List<CarUsersVO> list = iCarUsersService.exportCarUsers(carUsersVO);


        this.exportExcel(response, list, "导出会员信息");
    }

    /**
     * 添加会员信息
     *
     * @param carUsersVO
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加会员信息", notes = "添加会员信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carUsers:create")
    @RequestMapping(value = "/addCarUsers", method = RequestMethod.POST)
    public ResponseBean addCarUsers(@RequestBody CarUsersVO carUsersVO) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        carUsersVO.setCreateTime(new Date());
        carUsersVO.setCreateUser((String) obj);
        carUsersVO.setUpdateTime(new Date());
        carUsersVO.setUpdateUser((String) obj);
        carUsersVO.setDelFlag(DeleteFlagEnum.FALSE.VALUE);
        carUsersVO.setRegWay(CarUsersRegWayEnum.BACKGROUND_REGISTRATION.VALUE);
        carUsersVO.setUseFlag(CarUsersUserFalgEnum.ENABLE.VALUE);

        ResponseMessageVO responseMessageVO = iCarUsersService.addCarUsers(carUsersVO);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }
}
